'''
功能：封装了对mysql服务器增删改查功能



def close(self):#关闭Mysql链接

def select_all(self, sql, args = None):#查询所有

def select_one(self, sql, args = None):#查询单个

def insert_info(self, sql, args = None):#插入数据库

def delete_info(self, sql, args = None):#删除

def update_info(self, sql, args = None):#更新

'''

import pymysql



class ConnectMysql:

    def __init__(self):

        self.conn = pymysql.connect(
            host = '140.143.206.157',
            port = 3306,
            user = 'root',
            password = '123456',
            db = 'company',
            charset = 'utf8',
        )

        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        self.LID_SQL = 'SELECT LAST_INSERT_ID()'

    def close(self):#关闭链接

        self.cursor.close()

        self.conn.close()

    
    def select_all(self, sql, args = None):#查询所有

        if args == None:

            self.cursor.execute(sql)
        
        else:

            self.cursor.execute(sql, args)

        result_info = self.cursor.fetchall()

        return result_info

    def select_one(self, sql, args = None):#查询单个
    
        if args == None:

            self.cursor.execute(sql)
        
        else:

            self.cursor.execute(sql, args)

        result_info = self.cursor.fetchone()

        # print(result_info)

        return result_info

    def insert_info(self, sql, args = None):#插入数据库

        if args == None:

            self.cursor.execute(sql)
        
        else:

            self.cursor.execute(sql, args)

        
        ID = self.select_one(self.LID_SQL)['LAST_INSERT_ID()']#返回结果{'id':1},如果表没有id字段会出问题


        self.conn.commit()

        return ID

    def delete_info(self, sql, args = None):#删除

        if args == None:

            self.cursor.execute(sql)

        else:
    
            self.cursor.execute(sql, args)

        self.conn.commit()


        return {'message': 'ok'}

    def update_info(self, sql, args = None):#更新

        if args == None:
    
            self.cursor.execute(sql)

        else:
    
            self.cursor.execute(sql, args)

        self.conn.commit()


        return {'message': 'ok'}



    

    
        





if __name__ == '__main__':
    x = ConnectMysql()

    select_sql = 'select * from company limit 10'

    insert_sql = 'INSERT INTO consumption(company_id, consumption_url) value (1,"http://www.www.www")'

    delete_sql = 'delete from consumption where id = 157'

    # print(x.select_one(sql))

    # x.insert_info(insert_sql)

    print(x.delete_info(delete_sql))

    x.close()

    
    







